package com.yzt.zhuju.upload;

import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.mine.FileBean;
import com.yzt.p_base.AppUtils;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

/**
 * author : bob
 * date   : 2022/11/30
 * desc   : 资源文件上传
 */
public class UploadFileManager {

    private static class SingletonHolder {
        private static final UploadFileManager instance = new UploadFileManager();
    }

    public static UploadFileManager getInstance() {
        return SingletonHolder.instance;
    }

    /**
     * 上传图片
     *
     * @param filePath 文件路径
     * @param gender   性别 ： gender == 0 ,后台则不做校验处理
     */
    public void upload(String filePath, int gender, final UploadCallback uploadCallback) {

        Luban.with(AppUtils.getApp())
                .load(filePath)
                .ignoreBy(2000)
                .setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess(int index, File file) {
                        String mediaType = "image/jpeg";

                        if (filePath.contains("jpg")) {
                            mediaType = "image/jpg";
                        }

                        RequestBody requestFile = RequestBody.create(MediaType.parse(mediaType), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("uploadFile", file.getName(), requestFile);

                        BizLayer.getInstance().getAccountModule().uploadFile(body, gender, new AresSubscriber<FileBean>() {
                            @Override
                            protected void onSuccess(FileBean data) {
                                uploadCallback.uploadSuccess(data);
                            }

                            @Override
                            protected void onError(int code, String info) {
                                super.onError(code, info);
                                uploadCallback.uploadFail(code, info);
                            }
                        });
                    }

                    @Override
                    public void onError(int index, Throwable e) {

                    }

                }).launch();
    }

    /**
     * 上传视频
     *
     * @param filePath 文件路径
     */
    public void uploadVideo(String filePath, final UploadCallback uploadCallback) {

        File file = new File(filePath);

        RequestBody requestFile = RequestBody.create(MediaType.parse("video/mp4"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("uploadFile", file.getName(), requestFile);
        BizLayer.getInstance().getAccountModule().uploadFileVideo(body, new AresSubscriber<FileBean>() {
            @Override
            protected void onSuccess(FileBean data) {
                uploadCallback.uploadSuccess(data);
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
                uploadCallback.uploadFail(code, info);
            }
        });
    }

    public interface UploadCallback {

        void uploadSuccess(FileBean fileBean);

        void uploadFail(int code, String info);
    }
}

